package com.ctl.utils;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
/**
 * <p>Title: ZeroCopyUtil</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 *
 * @author ctl
 * @version 1.1
 * @date 2023-04-03 14:17
 */
public class ZeroCopyUtil {
    //零拷贝
    public static void main(String[] args) throws Exception {
        File file  = new File("/home/ctl/package.json");
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        FileChannel channel = raf.getChannel();
        //Opens a socket channel and connects it to a remote address.
        SocketChannel socketChannel = SocketChannel.open(
                new InetSocketAddress("127.0.0.1", 8888)
        );
        //Transfers bytes from this channel's file to the given writable byte channel.
        channel.transferTo(0,channel.size(), socketChannel);
    }
}




